package main

import (
	"errors"
	"fmt"
)

import (
	"context"
	"github.com/redis/go-redis/v9"
)

func main() {
	ctx := context.Background()

	rdb := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "123456", // no password set
		DB:       0,        // use default DB
	})

	err := rdb.Set(ctx, "key", "value", 0).Err()
	if err != nil {
		panic(err)
	}

	val, err := rdb.Get(ctx, "key").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println("key", val)

	val2, err := rdb.Get(ctx, "key2").Result()
	if errors.Is(err, redis.Nil) {
		fmt.Println("key2 does not exist")
	} else if err != nil {
		panic(err)
	} else {
		fmt.Println("key2", val2)
	}
	// Output: key value
	// key2 does not exist

	rdb.Set(ctx, "key2", "value2", 0)
	rdb.Get(ctx, "key2")
	rdb.Del(ctx, "key2")

	//
	rdb.HMSet(ctx, "hash", map[string]interface{}{
		"field1": "value1",
		"field2": "value2",
	})
	rdb.HGet(ctx, "hash", "field1")
	rdb.HGetAll(ctx, "hash")
	rdb.HDel(ctx, "hash", "field1")

	//
	rdb.SAdd(ctx, "set", "value1", "value2")
	rdb.SPop(ctx, "set")
	rdb.SRem(ctx, "set", "value1")

	//
	rdb.LPush(ctx, "list", "value1", "value2")
	rdb.LPop(ctx, "list")
	rdb.LRem(ctx, "list", 0, "value1")

}
